package com.xq.shop.controller;


import com.xq.shop.dto.*;
import com.xq.shop.service.IFileService;
import com.xq.shop.service.IOrderGoodsImageService;
import com.xq.shop.service.IOrderService;
import com.xq.shop.service.IUserService;
import com.xq.shop.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import utils.BasePage;
import utils.BaseResult;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author gaoxiaojin
 * @since 2024-07-30
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    private final IUserService userService;
    private final IOrderService orderService;
    private final IOrderGoodsImageService orderGoodsImageService;
    private final IFileService fileService;

    @Autowired
    public OrderController(IUserService userService, IOrderService orderService, IOrderGoodsImageService orderGoodsImageService, IFileService fileService) {
        this.userService = userService;
        this.orderService = orderService;
        this.orderGoodsImageService = orderGoodsImageService;
        this.fileService = fileService;
    }

    @RequestMapping("/page")
    public BaseResult<BasePage<OrderVo>> page(@RequestBody QueryOrderDto dto, HttpServletRequest request) {
        userService.getInfo(request);
        BaseResult<BasePage<OrderVo>> result = new BaseResult<>();
        BasePage<OrderVo> page = orderService.pageQuery(dto);
        result.initializeSuccess(page);
        return result;
    }

    @RequestMapping("/detail")
    public BaseResult<OrderDetailVo> detail(@RequestBody @Validated IntegerDto dto, HttpServletRequest request) {
        userService.getInfo(request);
        BaseResult<OrderDetailVo> result = new BaseResult<>();
        OrderDetailVo vo = orderService.detail(dto);
        result.initializeSuccess(vo);
        return result;
    }

    @RequestMapping("/editLabel")
    public BaseResult<Void> editLabel(@RequestBody @Validated EditLabelDto dto, HttpServletRequest request) {
        userService.getInfo(request);
        BaseResult<Void> result = new BaseResult<>();
        orderService.editLabel(dto);
        result.initializeSuccess();
        return result;
    }

    @RequestMapping("/goodsQuotationHistory")
    public BaseResult<List<GoodsQuotationVo>> goodsQuotationHistory(@RequestBody @Validated IntegerDto dto, HttpServletRequest request) {
        userService.getInfo(request);
        BaseResult<List<GoodsQuotationVo>> result = new BaseResult<>();
        List<GoodsQuotationVo> voList = orderService.goodsQuotationHistory(dto);
        result.initializeSuccess(voList);
        return result;
    }

    @RequestMapping("/goodsQuotation")
    public BaseResult<Void> goodsQuotation(@RequestBody @Validated QuotationDto dto, HttpServletRequest request) {
        UserVo vo = userService.getInfo(request);
        BaseResult<Void> result = new BaseResult<>();
        orderService.goodsQuotation(dto, vo.getId());
        result.initializeSuccess();
        return result;
    }


    @RequestMapping("/editOrder")
    public BaseResult<Void> editOrder(@RequestBody @Validated EditOrderDto dto, HttpServletRequest request) {
        UserVo vo = userService.getInfo(request);
        BaseResult<Void> result = new BaseResult<>();
        orderService.editOrder(dto, vo.getId());
        result.initializeSuccess();
        return result;
    }

    @RequestMapping("/sendMessage")
    public BaseResult<Void> sendMessage(@RequestBody @Validated OrderTrendsDto dto, HttpServletRequest request) {
        UserVo vo = userService.getInfo(request);
        BaseResult<Void> result = new BaseResult<>();
        orderService.sendMessage(dto, vo.getId());
        result.initializeSuccess();
        return result;
    }

    @RequestMapping("/removeMessage")
    public BaseResult<Void> removeMessage(@RequestBody @Validated IntegerDto dto, HttpServletRequest request) {
        UserVo vo = userService.getInfo(request);
        BaseResult<Void> result = new BaseResult<>();
        orderService.removeMessage(dto, vo.getId());
        result.initializeSuccess();
        return result;
    }

    @RequestMapping("/goodsProcure")
    public BaseResult<Void> goodsProcure(@RequestBody @Validated GoodsProcureDto dto, HttpServletRequest request) {
        UserVo vo = userService.getInfo(request);
        BaseResult<Void> result = new BaseResult<>();
        orderService.goodsProcure(dto, vo.getId());
        result.initializeSuccess();
        return result;
    }

    @RequestMapping("/getGoodsProcure")
    public BaseResult<Map<String, List<GoodsProcureVo>>> getGoodsProcure(@RequestBody @Validated StringDto dto, HttpServletRequest request) {
        userService.getInfo(request);
        BaseResult<Map<String, List<GoodsProcureVo>>> result = new BaseResult<>();
        Map<String, List<GoodsProcureVo>> map = orderService.getGoodsProcure(dto);
        result.initializeSuccess(map);
        return result;
    }

    @RequestMapping("/removeGoodsProcure")
    public BaseResult<Void> removeGoodsProcure(@RequestBody @Validated IntegerDto dto, HttpServletRequest request) {
        userService.getInfo(request);
        BaseResult<Void> result = new BaseResult<>();
        orderService.removeGoodsProcure(dto);
        result.initializeSuccess();
        return result;
    }

    @RequestMapping("/refund")
    public BaseResult<Void> refund(@RequestBody @Validated RefundDto dto, HttpServletRequest request) {
        UserVo vo = userService.getInfo(request);
        BaseResult<Void> result = new BaseResult<>();
        orderService.refund(dto, vo.getId());
        result.initializeSuccess();
        return result;
    }

    @RequestMapping("/removeRefund")
    public BaseResult<Void> removeRefund(@RequestBody @Validated IntegerDto dto, HttpServletRequest request) {
        userService.getInfo(request);
        BaseResult<Void> result = new BaseResult<>();
        orderService.removeRefund(dto);
        result.initializeSuccess();
        return result;
    }

    @RequestMapping("/queryRefund")
    public BaseResult<List<RefundVo>> queryRefund(@RequestBody @Validated IntegerDto dto, HttpServletRequest request) {
        userService.getInfo(request);
        BaseResult<List<RefundVo>> result = new BaseResult<>();
        List<RefundVo> voList = orderService.queryRefund(dto);
        result.initializeSuccess(voList);
        return result;
    }

    @RequestMapping("/queryDelivery")
    public BaseResult<List<GoodsDeliveryVo>> queryDelivery(@RequestBody @Validated StringDto dto, HttpServletRequest request) {
        userService.getInfo(request);
        BaseResult<List<GoodsDeliveryVo>> result = new BaseResult<>();
        List<GoodsDeliveryVo> voList = orderService.queryDelivery(dto);
        result.initializeSuccess(voList);
        return result;
    }

    @RequestMapping("/delivery")
    public BaseResult<Void> delivery(@RequestBody @Validated DeliveryDto dto, HttpServletRequest request) {
        UserVo userVo = userService.getInfo(request);
        BaseResult<Void> result = new BaseResult<>();
        orderService.delivery(dto, userVo.getId());
        result.initializeSuccess();
        return result;
    }

    @RequestMapping("/removeDelivery")
    public BaseResult<Void> removeDelivery(@RequestBody @Validated IntegerDto dto, HttpServletRequest request) {
        UserVo userVo = userService.getInfo(request);
        BaseResult<Void> result = new BaseResult<>();
        orderService.removeDelivery(dto, userVo.getId());
        result.initializeSuccess();
        return result;
    }

    @RequestMapping("/uploadOrderGoodsImage")
    public BaseResult<FileVo> upload(@RequestParam("file")MultipartFile file, @RequestParam("orderId") Integer orderId,@RequestParam("orderGoodsId") Integer orderGoodsId, HttpServletRequest request) {
        UserVo userVo = userService.getInfo(request);
        BaseResult<FileVo> result = new BaseResult<>();
        FileVo fileVo = fileService.upload(file);
        orderGoodsImageService.addOrderGoodsImage(orderId,orderGoodsId,fileVo.getId(),userVo.getId());
        result.initializeSuccess(fileVo);
        return result;
    }

    @RequestMapping("/removeOrderGoodsImage")
    public BaseResult<Void> removeRefund(@RequestBody IntegerDto dto) {
        BaseResult<Void> result = new BaseResult<>();
        orderGoodsImageService.removeOrderGoodsImage(dto);
        result.initializeSuccess();
        return result;
    }

}
